package kz.pompei.ifacer.dialogs;

import static kz.pompei.ifacer.dialogs.DiaUtil.extractWindow;

import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ButtonDialog {
  
  private final Component parentComponent;
  private int result;
  
  private final JPanel pane;
  private String title;
  private JDialog dia;
  private JButton defaultButton;
  private GridBagConstraints gbc;
  public int buttonWidth = 200;
  public int buttonHeight = 50;
  public String dialogName = "CommonButtonDialog";
  
  public ButtonDialog(Component parentComponent, int defaultResult) {
    this.parentComponent = parentComponent;
    result = defaultResult;
    
    pane = new JPanel();
    pane.setLayout(new GridBagLayout());
    gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.insets = new Insets(5, 5, 5, 5);
  }
  
  public void setTitle(String title) {
    this.title = title;
  }
  
  public void addMessage(String message) {
    JLabel label = new JLabel("<html><p align=center>" + message.replaceAll("\\\\n", "<br />")
        + "</p></html>");
    pane.add(label, gbc);
    gbc.gridy++;
  }
  
  public void addButton(final int clickResult, String caption) {
    addButton(caption, clickResult, false);
  }
  
  public void addDefButton(String caption, final int clickResult) {
    addButton(caption, clickResult, true);
  }
  
  public void addButton(String caption, final int clickResult, boolean isDefaultButton) {
    final JButton b = new JButton("<html><p align=center>" + caption.replaceAll("\\\\n", "<br />")
        + "</p></html>");
    pane.add(b, gbc);
    gbc.gridy++;
    b.setPreferredSize(new Dimension(buttonWidth, buttonHeight));
    
    b.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        result = clickResult;
        endDialog();
      }
    });
    
    b.addKeyListener(new KeyAdapter() {
      @Override
      public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
          b.doClick(100);
        }
      }
    });
    
    if (isDefaultButton) defaultButton = b;
  }
  
  private void endDialog() {
    dia.setVisible(false);
  }
  
  public int show() {
    final Window window;
    dia = new JDialog(window = extractWindow(parentComponent), title, ModalityType.DOCUMENT_MODAL);
    
    dia.setContentPane(pane);
    
    DiaUtil.makeEscapeKey(dia);
    
    DiaUtil.put(dia, dialogName, window, 100, 100, 800, 600);
    
    if (defaultButton != null) {
      SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
          defaultButton.requestFocusInWindow();
        }
      });
    }
    
    dia.setVisible(true);
    return result;
  }
}
